
************************************************************************************************
* Set up data set to "Public Health Policy At Scale.."
************************************************************************************************


clear 
set more off 
set matsize 8000 
cap log close 

cd "D:\Data\workdata\707116\SIDS_RDD"


use "D:/Data/Workdata/707116/ChildVax/02_temp/Nov2019/mortality2.dta", clear
tempfile t1 
save `t1' 

use "D:/Data/Workdata/707116/ChildVax/02_temp/Nov2019/maternal_ses.dta", clear 



bysort mpnr_mfr: egen max_year = max(grundviveyear)
keep if max_year ==grundviveyear 
drop max_year
tempfile t2 
save `t2' 


use "D:/Data/Workdata/707116/ChildVax/02_temp/Nov2019/births19732014v2.dta", clear 

merge 1:1 pnr using "D:/Data/Workdata/707116/ChildVax/02_temp/Nov2019/smoking1991_2014.dta"
assert _merge!=2 
drop _merge 

merge 1:1 pnr  using `t1' 
assert _merge!=2 
drop _merge



merge m:1 mpnr_mfr  using `t2'
tab byear _merge 
drop if _merge==2 
drop _merge 

g bym = ym(byear, bmonth) 
format bym %tm 
g po_start = ym(1992,1)
format po_start %tm

gen dif = bym - po_start 
*check if worked 

table bym, c(mean dif)

gen treat = 0 
replace treat=1 if dif>=0
 
gen di1 = dif 
gen di1_i=dif 
replace di1_i=0 if dif<0

gen di2 = dif^2 
gen di2_i = dif^2 
replace di2_i=0 if dif<0 

g dc_all_other = dc_unknown + dc_mis + dc_oth 

global causes "dc_inf dc_neo dc_nerv dc_resp dc_pre_nat dc_cog_mal dc_sids dc_cog_mal dc_all_other"

foreach i in $causes{
replace `i' = 0 if `i'==. 
replace `i' = 0 if `i'==1 & deathdate>=bday_5yr
replace `i' = `i'*10000 if `i'==1 & deathdate<bday_5yr
sum `i'
}

g 		mor1 = 0 
replace mor1 = 10000 if dead==1 & deathdate<bday_1yr

g       mor5 = 0 
replace mor5 = 10000 if dead==1 & deathdate<bday_5yr

g       border2 = border 
replace border2 =4 if border>3

drop if byear>2006 

save "SIDS_analysis_final_full.dta", replace
 
